﻿using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using Rison.DataAccess.Data.ZJCP;

namespace Rison.Web.ZJCP.ashx
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class load_convenience_qa : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string qaId = context.Request.Params["qaId"];
            if (String.IsNullOrEmpty(qaId))
            {
                context.Response.Write("false");
            }
            else
            {
                context.Response.Write(loadConvenienceQa(qaId));
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        private string loadConvenienceQa(string qaId)
        {
            string sql = String.Format("select qa_url, qa_question, qa_answer, qa_last_save_time from ConvenienceQa where qa_id='{0}'", qaId);
            DataSet ds = DbAccess.executeQuery(sql);
            string qaUrl = ds.Tables[0].Rows[0][0].ToString();
            string qaQuestion = ds.Tables[0].Rows[0][1].ToString();
            string qaAnswer = ds.Tables[0].Rows[0][2].ToString();
            string lastSaveTime = ds.Tables[0].Rows[0][3].ToString();
            qaUrl = replaceSpicalCharacter(qaUrl);
            qaQuestion = replaceSpicalCharacter(qaQuestion);
            qaAnswer = replaceSpicalCharacter(qaAnswer);
            lastSaveTime = replaceSpicalCharacter(lastSaveTime);
            return String.Format("{{qaUrl:\"{0}\",qaQuestion:\"{1}\",qaAnswer:\"{2}\", lastSaveTime:\"{3}\"}}", qaUrl, qaQuestion, qaAnswer,lastSaveTime).ToString();

        }
        private string replaceSpicalCharacter(string str)
        {
            if (!String.IsNullOrEmpty(str))
            {
                str = str.Replace("\n", "\\n");
                str = str.Replace("\r", "\\r");
                str = str.Replace("\"", "\\\"");
            }
            return str;
        }
    }
}
